package com.vision.sales.sv.serv.rpc;

import static com.vision.sales.cm.data.SalesPermissions.CreateProduct;
import static com.vision.sales.cm.data.SalesPermissions.DeleteProduct;
import static com.vision.sales.cm.data.SalesPermissions.UpdateProduct;
import static com.vision.sales.cm.data.SalesPermissions.ViewProduct;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.vision.core.cm.data.Domain;
import com.vision.core.cm.exception.RpcException;
import com.vision.core.sv.db.mgr.IDataManager;
import com.vision.core.sv.db.mgr.ISimpleCriteria;
import com.vision.core.sv.db.mgr.SimpleCriteria;
import com.vision.core.sv.serv.rpc.AbstractServiceImpl;
import com.vision.core.sv.util.SimpleTransformer;
import com.vision.sales.cm.db.data.Product;
import com.vision.sales.cm.serv.rpc.ProductService;

/**
 * 
 * @author Mark
 *
 */
@Service("products")
public class ProductServiceImpl extends AbstractServiceImpl<Product> implements ProductService {

	private static final long serialVersionUID = 1L;
	
	public ProductServiceImpl() {
		initGilead();
	}

	@Autowired
	@Qualifier("productManager")
	@Override
	public void setDataManager(IDataManager<Product> dataManager) {
		super.setDataManager(dataManager);
	}
	
	@Override
	protected Domain getDomain() {
		return Domain.PRODUCT;
	}
	
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	@Override
	public List<Product> getAll() throws RpcException {
		checkPermissions(ViewProduct);
		
		return super.getAll();
	}
	
	@Transactional
	@Override
	public Product create(Product product) throws RpcException {
		checkPermissions(CreateProduct);
		
		Product result = super.create(product);
		
		logCreate(result);
		
		return result;
	}
	
	@SuppressWarnings({ "unchecked" })
	@Override
	public List<Product> getProductNameAndIdOnly() throws RpcException {
		checkPermissions(ViewProduct);
		
		ISimpleCriteria crit = SimpleCriteria.forClass(Product.class);
		String[] properties = new String[]{ "id", "code", "name" };
		crit.properties(properties); // Retrieve only id and name
		crit.asc("name");
		crit.setResultTransformer(SimpleTransformer.create(Product.class, properties));
		
		List<Product> list = getTemplate().findByCriteria(crit.getDetached());
		return list;
	}
	
	@Transactional
	@Override
	public Product update(Product product) throws RpcException {
		checkPermissions(UpdateProduct);
		
		Product existing = checkExisting(product.getId());
		Product result = super.update(product);
		
		logUpdate(existing, result);
		
		return result;
	}
	
	@Transactional
	@Override
	public Product delete(Product product) throws RpcException {
		checkPermissions(DeleteProduct);
		
		Product result = super.delete(product);
		
		logDelete(result);
		
		return result;
	}
	
}
